﻿package com.ui.controls
{
	import com.ui.cell.LabelSource;
	import com.ui.core.Align;
	import com.ui.core.UIComponent;
	import com.ui.data.IconData;
	import com.ui.data.LabelData;
	import com.ui.manager.UIManager;

	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.geom.Rectangle;
	import flash.text.StyleSheet;
	import flash.text.TextField;

	/**
	 * Game Label
	 * 
	 * @author Cafe
	 * @version 20100723
	 */
	public class Label extends UIComponent
	{
		protected var _data:LabelData;
		protected var _icon:Icon;
		protected var _textField:TextField;
		private var _text:String;

		override protected function create():void
		{
			if ( _data.iconData)
			{
				_icon = new Icon( _data.iconData );
			}
			else
			{
				mouseEnabled = false;
				mouseChildren = false;
			}
			_textField = UIManager.getTextField();
			// _textField.border=true;
			_textField.mouseEnabled = false;
			_textField.selectable = false;
			_textField.autoSize = _data.autoSize;
			_textField.defaultTextFormat = _data.textFormat;
			_textField.styleSheet = _data.styleSheet;
			_textField.textColor = _data.textColor;
			_textField.filters = _data.textFieldFilters;
			if (_data.text)
				_textField.text = _data.text;
			if (_data.htmlText)
				_textField.htmlText = _data.htmlText;
			if (_data.htmlText != "")
				_textField.htmlText = _data.htmlText;
			if (_data.textFieldAlpha < 1)
			{
				blendMode = BlendMode.LAYER;
				_textField.alpha = _data.textFieldAlpha;
			}
			if (_data.width > 0)
			{
				_textField.width = _data.width;
				_textField.wordWrap = true;
			}
			if (_icon)
			{
				addChild( _icon );
				_icon.mouseEnabled = _data.mouseEnabled;
			}
			if (_data.toolTipData)
			{
				mouseEnabled = true;
				_textField.mouseEnabled = true;
			}
			addChild( _textField );
		}

		private function autoLayout():void
		{
			var textW:int = 0;
			var textH:int = 0;
			var rect:Rectangle = new Rectangle( 0, 0, _textField.width, _textField.height );
			if (_textField.text.length > 0)
			{
				textW = rect.x * 2 + rect.width;
				textH = rect.y * 2 + rect.height;
			}
			_width = iconW + _data.hGap + textW;
			_height = Math.max( iconH, textH );

			if (_icon)
			{
				var iconW:int = _icon.width;
				var iconH:int = _icon.height;
				if (_data.iconY == 0)
				{
					_icon.y = int( (_height - iconH) * 0.5 );
				}
				else
				{
					_icon.y = _textField.y + rect.y + rect.height + _data.iconY - iconH;
				}
				_textField.x = (iconW == 0 ? 0 : iconW + _data.hGap);
				_textField.y = Math.floor( (_height - textH) * 0.5 );
				switch(_data.align)
				{
					case Align.CENTER:
						_textField.x = int( _data.width / 2 - _textField.textWidth / 2 );
						_textField.y = int( _data.height / 2 - textField.textHeight / 2 );
						break;
				}
			}
		}

		override protected function layout():void
		{
			autoLayout();
			// switch(_data.scaleMode){
			// case ScaleMode.AUTO_SIZE:
			// autoLayout();
			// break;
			// case ScaleMode.SCALE_NONE:
			// noneLayout();
			// break;
			// }
		}

		// private function noneLayout() : void {
		// _icon.moveTo(_data.iconData.x, _data.iconData.y);
		// //  _textField.x=_data.
		// }
		public function Label( data:LabelData )
		{
			_data = data;
			super( data );
		}

		public function get icon():Icon
		{
			return _icon;
		}

		override public function get width():Number
		{
			if (_icon) return textField.textWidth;
			return textField.width;
		}

		override public function get height():Number
		{
			return textField.height;
		}

		public function set iconBitmapData( value:BitmapData ):void
		{
			if (!_icon)
			{
				_icon = new Icon( new IconData() );
			}
			_icon.bitmapData = value;
			layout();
		}

		public function set styleSheet( value:StyleSheet ):void
		{
			_textField.styleSheet = value;
		}

		public function set text( value:String ):void
		{
			if (_text != value)
			{
				_text = value;
				if (_data.enterHandler) value = textToFlashTF( value );
				_textField.text = value;
				// GStringUtil.truncateToFit( value, _data.maxLength );
				layout();
			}
		}

		override public function set enabled( value:Boolean ):void
		{
			super.enabled = value;
		}

		public function get text():String
		{
			return _textField.text;
		}

		public function set textColor( value:uint ):void
		{
			_textField.textColor = value;
		}

		public function set htmlText( value:String ):void
		{
			if (_data.enterHandler) value = textToFlashTF( value );
			_textField.htmlText = value;
			layout();
		}

		private function textToFlashTF( text:String ):String
		{
			return text.split( '\r\n' ).join( '\r' );
		}

		public function get htmlText():String
		{
			return _textField.htmlText;
		}

		public function clear():void
		{
			text = "";
		}

		public function get textHeight():int
		{
			return _textField.textHeight;
		}

		public function get textWidth():int
		{
			return _textField.textWidth;
		}

		override public function set source( value:* ):void
		{
			var data:LabelSource = value as LabelSource;
			if (data == null)
			{
				clear();
			}
			else
			{
				text = data.text;
			}
			_source = data;
		}

		public function get textField():TextField
		{
			return _textField;
		}
	}
}
